package nexus.com.textfield
{
	import com.ux.manager.StageManager;
	import com.ux.util.TextFieldUtil;
	
	import flash.display.Sprite;
	import flash.events.MouseEvent;
	import flash.text.TextField;
	import flash.ui.Mouse;
	import flash.ui.MouseCursor;
	
	import nexus.manager.ToolTipManager;
	
	
	
	/**
	 * <br>설명 : </br> 
	 * @author winnie
	 */	
	public class TruncateTextField extends TextField
	{
		//-----------------------------------------------------------------------------------
		//
		// private vars
		//
		//-----------------------------------------------------------------------------------
		
		private var _truncated : Boolean = false;
		private var _enableTruncat : Boolean = true;
		private var _originString : String = "";
		private var _truncateMark : String = "...";
		private var _widthToTruncate : Number = -1;
		
		//-----------------------------------------------------------------------------------
		//
		// protected vars
		//
		//-----------------------------------------------------------------------------------
		
		
		//-----------------------------------------------------------------------------------
		//
		// public vars
		//
		//-----------------------------------------------------------------------------------
		
		
		
		//-----------------------------------------------------------------------------------
		//
		// constructor
		//
		//-----------------------------------------------------------------------------------
		public function TruncateTextField()
		{
			super();
		}
		
		
		//-----------------------------------------------------------------------------------
		//
		// setter / getter
		//
		//-----------------------------------------------------------------------------------

		public function get widthToTruncate():Number
		{
			return _widthToTruncate;
		}

		public function set widthToTruncate(value:Number):void
		{
			_widthToTruncate = value;
		}

		public function get originString():String
		{
			return _originString;
		}
		
		public function get truncateMark():String
		{
			return _truncateMark;
		}
		
		public function set truncateMark(value:String):void
		{
			_truncateMark = value;
		}
		
		public function get enableTruncat():Boolean
		{
			return _enableTruncat;
		}
		
		public function set enableTruncat(value:Boolean):void
		{
			_enableTruncat = value;
		}
		
		public function set truncated(value:Boolean):void
		{
			_truncated = value;
		}
		
		public function get truncated():Boolean
		{
			return _truncated;
		}

		
		//-----------------------------------------------------------------------------------
		//
		// public function
		//
		//-----------------------------------------------------------------------------------
		
		
		public function setText( str:String ):void
		{
			_originString = str;
			setTruncateText();
		}
		
		public function apply():void
		{
			setTruncateText();
		}
		
		//-----------------------------------------------------------------------------------
		//
		// protected function
		//
		//-----------------------------------------------------------------------------------
		
		
		
		
		//-----------------------------------------------------------------------------------
		//
		// private function
		//
		//-----------------------------------------------------------------------------------
		
		private function setTruncateText():void
		{
			this.text = originString;
			truncated = TextFieldUtil.truncateByWidth( this, widthToTruncate, truncateMark );
			
			if( truncated )
			{
				this.addEventListener(MouseEvent.ROLL_OVER, onOver );
				this.addEventListener(MouseEvent.ROLL_OUT, onOut );
			}
			else
			{
				this.removeEventListener(MouseEvent.ROLL_OVER, onOver );
				this.removeEventListener(MouseEvent.ROLL_OUT, onOut );
			}
		}
		
		
		
		//-----------------------------------------------------------------------------------
		//
		// event handler
		//
		//-----------------------------------------------------------------------------------
		
		
		protected function onOut(e:MouseEvent):void
		{
			Mouse.cursor = MouseCursor.AUTO;
			ToolTipManager.instance.hideTooltip();
		}
		
		protected function onOver(e:MouseEvent):void
		{
			Mouse.cursor = MouseCursor.ARROW;
			ToolTipManager.instance.showTooltip( originString );
		}		
		
		
		//-----------------------------------------------------------------------------------
		//
		// destory
		//
		//-----------------------------------------------------------------------------------
		public function destory():void
		{
			
		}

	}
}